<?php
namespace libraries;

/**
 * @author fzq
 * @date 2016-09-08
 * @comment 知信发送类( 由于服务器将采用swoole故此处不再做过多的动态加载处理 )
 * 涉及到的文件及目录如下：config里的sms.php, libraries/sms/中的Trait, libraries中的Sms.php
 * services.php中配置如下:
 * 
 * $di->setShared( 'sms', function() use( $di ){
 *	$sms = Sms::getInstance( $di );
 *
 *	return $sms;
 * });
 * 
 * 使用方法:
 * $sms = $di->get( 'sms' );
 * $sms->getParam( Sms::BIZ_REGISTER );
 * $param[ 'mobile' ] = '15502921927';
 * $param['params'] = array( 'name' => 'bruce', 'code' => 'h2x8', 'hour' => 1 ); 
 * 
 * var_dump( $sms->sendSms( $sms ));
 */
class Sms implements \Phalcon\DI\InjectionAwareInterface
{
	use \libraries\sms\TJuHeSms;
	
	/**
	 * 重置密码
	 */
	const BIZ_RESET_PASS = 'reset_pass';
	
	/**
	 * 
	 * 会员注册验证码
	 */
	const BIZ_REGISTER = 'register';
	
	/**
	 *
	 * 博物馆审核通过
	 */
	const BIZ_MUSEUM_EXAMINE = 'museum_examine';
	
	/**
	 * 找回密码
	 */
	const BIZ_RETRIEVE_PASS = 'retrieve_pass';
	
	private $_di;
	private $_smsCfg;
	private $_defaultSmsCfg;
// 	private $_redis;
	
	/**
	 * 服务提供商
	 */
	private $_arrServProvider;
	private static $_instance = null;
	
	public static function getInstance( $di )
	{
		if( !Sms::$_instance )
		{
			Sms::$_instance = new Sms( $di );
		}
			
		return self::$_instance;
	}
	
	private function __construct( $di )
	{
		if( $di != null )
		{
			$this->_di = $di;
			
			$this->_redis = $this->_di->get( 'nredis' ); 
		}
		
		if( file_exists( APP_ROOT . 'config/' . APP_MODE . '/sms.php' ))
		{
			$this->_smsCfg = include_once APP_ROOT . 'config/' . APP_MODE . 'sms.php';
		}
		else
		{
			$this->_smsCfg = include_once APP_ROOT . 'config/sms.php';
		}
		
		$this->_defaultSmsCfg = $this->_smsCfg->{ $this->_smsCfg->default };
	}
	
	/**
	 * get param
	 * @param string $bizType
	 */
	public function getParam( $bizType = 'register' )
	{
// 		try 
// 		{
			$arrParams['params'] = $this->_smsCfg->{$this->_smsCfg->default}->{ 'tpl_' . $bizType }->params;
			$arrParams[ 'tplName' ] = $bizType;
			$arrParams[ 'mobile' ] = '这里写上手机号码';
			
			return $arrParams;
// 		}
// 		catch( \Exception $e )
// 		{
// 			echo $e->getTraceAsString();			
// 		}
	}
	
	/**
	 * 发送SMS信息
	 * @param string $strTo
	 * @param array $arrParams
	 * @param int $iType
	 */
	public function sendSms( $arrParams )
	{
		$func = 'sendSms' . $this->_smsCfg->default;
		return $this->{$func}( $arrParams );	
	}
	
// 	/**
// 	 * 
// 	 */
// 	private function isLimited( $arrParams )
// 	{
// 		if( $this->_redis->get( '' ) )
// 		{
			
// 		}
// 	}
	
	/* (non-PHPdoc)
	 * @see \Phalcon\Di\InjectionAwareInterface::setDI()
	 */
	public function setDI(\Phalcon\DiInterface $dependencyInjector) 
	{
		// TODO Auto-generated method stub
		$this->_di = $dependencyInjector;
		
		$this->_redis = $this->_di->get( 'nredis' );
		
	}

	/* (non-PHPdoc)
	 * @see \Phalcon\Di\InjectionAwareInterface::getDI()
	 */
	public function getDI() 
	{
		// TODO Auto-generated method stub
		return $this->_di;
	}
}

// $sms = new Sms();
// $sms->test();
